﻿//-----------------------------------------------------------------------------------
// Detailed Extraction Rule Template version 1.0
//-----------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace $rootnamespace$
{
    [System.ComponentModel.DisplayName("$itemname$")]
    [System.ComponentModel.Description("TODO: Add a more detailed description of $itemname$ extraction rule.")]
    public class $safeitemname$ : ExtractionRule
    {
        #region Constructors
        /// <summary>
        /// standard constructor
        /// </summary>
        public $safeitemname$() { }
        #endregion

        #region Properties
        /// <summary>
        /// If True, the extraction rule fails if not found.
        /// </summary>
        bool m_required;
        [System.ComponentModel.DisplayName("Required")]
        [System.ComponentModel.Description("If True, the extraction rule fails if not found.")]
        [System.ComponentModel.DefaultValue(true)]
        public bool Required 
        {
            get { return m_required; }
            set { m_required = value; }
        }

        /// <summary>
        /// Contains value that was extracted.
        /// </summary>
        string m_extractedValue;
        public string ExtractedValue
        {
            get { return m_extractedValue; }
        }

        /// <summary>
        /// Contains a message that is surfaced to the user
        /// </summary>
        string m_message;
        public string ExtractMessage
        {
            get { return m_message; }
        }

        /*
         * Public properties on the class will be surfaced to the user in the VS UI.
         * Only string, int, and bool types are allowed.
         */

        /*
            /// <summary>
            /// Sample string property
            /// </summary>
            private string m_myStringProperty;
            [System.ComponentModel.DisplayName("My String Property")]
            [System.ComponentModel.Description("Enter a long description for this property.")]
            [System.ComponentModel.DefaultValue("MyString")]
            public string MyStringProperty
            {
                get { return m_myStringProperty; }
                set { m_myStringProperty = value; }
            }
        */

        #endregion // Properties

        #region Utility Functions
        /// <summary>
        /// A sample utility function that performs the actual work of extracting values. 
        /// This demonstrates the best practice of moving most of the code out into 
        /// it's own function so you can test it from a unit test.
        /// </summary>
        /// <returns>true - value found, false - value not found, reason contains more details</returns>
        public bool Extract(string document)
        {
            Debug.Assert(document != null);
            // TODO:  Implement your extraction code here
            throw (new NotImplementedException("ExtractionRule1:MyExtractionFunction not implemented !"));

            // m_extractedValue = "Value found";
            // m_message = "Additional information regarding success/failure of function";
            // return true; // true if found, false if not found
        }
        #endregion

        #region Extract Method
        /// <summary>
        /// The Extract method.  The parameter e contains the Web test context.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            try
            {
                e.Success = Extract(e.Response.BodyString);
                if (e.Success)
                {
                    e.Success = true;
                    e.WebTest.Context[this.ContextParameterName] = ExtractedValue;
                }
                else
                {
                    // If the extraction fails, set the error text that the user sees
                    e.Message = String.Format(CultureInfo.CurrentCulture, ExtractMessage);
                    //
                    // If Not required, then not finding it is ok
                    if (!Required) e.Success = true;
                }
            }
            catch (Exception ex)
            {
                e.Message = String.Format(CultureInfo.CurrentCulture, "{0}[{1}] Exception: {2}", "ExtractionRule1", this.ContextParameterName, ex.Message.ToString());
                e.Success = false;
            }
        }
        #endregion
    }
}
